package com.aruba.bulethoothdemoapplication.view;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.aruba.bulethoothdemoapplication.adapter.MyItemRecyclerViewAdapter;
import com.aruba.bulethoothdemoapplication.databinding.FragmentItemListBinding;
import com.aruba.bulethoothdemoapplication.model.BleDevice;
import com.aruba.bulethoothdemoapplication.viewmodel.SelectDeviceViewModel;

import java.util.List;

public class SelectDeviceFragment extends Fragment {
    private OnListFragmentInteractionListener mListener;
    private SelectDeviceViewModel selectDeviceViewModel;
    public static final int REQUEST_ENABLE_BT = 385;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        FragmentItemListBinding binding = FragmentItemListBinding.inflate(inflater, container, false);

        selectDeviceViewModel = new ViewModelProvider(getActivity(), ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication())).get(SelectDeviceViewModel.class);
        
        binding.list.setLayoutManager(new LinearLayoutManager(getContext()));
        MyItemRecyclerViewAdapter adapter = new MyItemRecyclerViewAdapter(mListener);
        binding.list.setAdapter(adapter);
        
        selectDeviceViewModel.datas.observe(getActivity(), new Observer<List<BleDevice>>() {
            @Override
            public void onChanged(List<BleDevice> bleDevices) {
                adapter.setValues(bleDevices);
                adapter.notifyDataSetChanged();
            }
        });
        
        getLifecycle().addObserver(selectDeviceViewModel);
        return binding.getRoot();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnListFragmentInteractionListener) {
            mListener = (OnListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!selectDeviceViewModel.isBluetoothEnabled()) {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, REQUEST_ENABLE_BT);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_ENABLE_BT) {
            if(resultCode == Activity.RESULT_OK) {
                selectDeviceViewModel.scanDevice();
            } else {
                toast("蓝牙未开启！");
            }
        }
    }

    
    public interface OnListFragmentInteractionListener {
        void onListFragmentInteraction(BleDevice item);
    }

    private void toast(String text){
        Toast.makeText(getActivity(),text,Toast.LENGTH_SHORT).show();
    }
}
